/**
 * Created by DELL on 2017/6/13.
 */
common.buildNormalEnumSelect(".wifiOnlineStatus", enumObject.enumJsonData['wifiOnlineStatus']);//创建 门锁在线状态 枚举列表
/**
 * 中介联系人 列表初始化设置
 * @type {Object}
 */
var settings = {
    "sAjaxSource": window.globalHost + common.lockListPath,
    "aoColumns": [{
        "mDataProp": function(obj) {
            return "<a href='../submenu.html?key=lockRecord&id=" + obj.id + "&devId="+obj.deviceId+"'>点击进入开锁记录</a>";
        },
        "sTitle": "开锁记录",
        "sDefaultContent": ""
    },{
        "mDataProp": function(obj) {
            return (obj.contractNo?obj.contractNo:"无编号");
        },
        "sTitle": "房源合同编号",
        "sDefaultContent": "",
        "sClass": "center"
    },{
        "mDataProp": function(obj) {
            return (!!obj.villageName?obj.villageName:"无信息")+"<br/>"+(obj.houseAddress?obj.houseAddress:"无信息");
        },
        "sTitle": "小区/房源地址",
        "sDefaultContent": ""
    },{
        "mDataProp": function(obj) {
            return  (!!obj.powerLeftValue?obj.powerLeftValue+"":"无电量")+"<br/>"+(!!obj.wifiOnlineStatus?enumObject.enumJsonData['wifiOnlineStatus'][obj.wifiOnlineStatus]:"无在线状态");
        },
        "sTitle": "门锁电量/线上状态",
        "sDefaultContent": ""
    },{
        "mDataProp": function(obj) {
            return obj.userName+"<br/>"+obj.createTime;
        },
        "sTitle": "操作人 / 创建时间",
        "sDefaultContent": ""
    },{
        "mDataProp": 'password',
        "sTitle": "当前密码",
        "sDefaultContent": "",
        "sClass":"text-center"
    },{
        "mDataProp": function(obj) {
            return "<a href='javascript:;' class='btn btn-success setPwd' data-pwd='"+obj.password+"' data-id='"+obj.id+"' data-houseAddress='"+obj.houseAddress+"' data-contractNo='"+obj.contractNo+"'>设置密码</a>";
        },
        "sTitle": "设置密码",
        "sDefaultContent": "",
        "sClass":"text-center"
    },{
        "mDataProp": function(obj) {
            return "<a href='javascript:;' class='btn btn-warning getLockInfo' data-id='"+obj.id+"'>同步锁信息</a>";
        },
        "sTitle": "同步锁信息",
        "sDefaultContent": "",
        "sClass":"text-center"
    },{
        "mDataProp": function(obj) {
            return '<a href="javascript:;" class="btn btn-danger lock-open" data-id="'+obj.id+'" style="margin:0 15px;">一键开锁</a>';
        },
        "sTitle": "一键开锁",
        "sDefaultContent": "",
        "sClass":"text-center"
    }],
    "fnServerParams": function(aoData) {
        return common.getSearchData("form.data-list-search",aoData);
    }
};
var options = $.extend(true, {}, _dataTableSettings, settings);
/**
 * 实例化 中介联系人 列表插件
 * @type {[type]}
 */
var $currentDataTable=$("#data-list");
var oTable = $currentDataTable.dataTable(options);
searchList.getDataList(oTable);
searchList.resetSearch(oTable);
var userInfo=common.getLocalData(common.agentSysCacheWord).user;
var setPassword='<div style="margin-top: 10px;padding: 0 20px;" id="menu-list-form">'+
                    '<form>'+
                        '<p  class="clearfix"><label class="col-xs-3 text-center">房源编号</label><span class="col-xs-8" style="color:#212121;line-height: 34px;"  id="contractNo"></span></p>'+
                        '<p  class="clearfix"><label class="col-xs-3 text-center">房源地址</label><span class="col-xs-8" style="color:#212121;line-height: 34px;"  id="houseAddress"></span></p>'+
                        '<p  class="clearfix"><label class="col-xs-3 text-center">长期密码</label><input class="col-xs-8" type="text" required maxlength="6" name="password" placeholder="请输入6位数字密码"></p>'+
                    '</form>'+
                '</div>';

var regPwd=/^\d{6}$/;
$currentDataTable.off("click",".setPwd").on("click",".setPwd",function (e) {
    var $this=$(this);
    var lockId=$this.attr("data-id");
    var houseAddress=$this.attr("data-houseAddress");
    var contractNo=$this.attr("data-contractNo");
    var pwd=$this.attr("data-pwd");
    var sendData={};
    common.openTempDialog({
        type: 1,
        title: '设置密码',
        area: ['600px', '300px'],
        content: setPassword,
        FailedMsg: "请输入6位数字",
        sendData: sendData,
        ajaxUrl: window.globalHost+common.setlockPwdPath,
        contentOnLoad: function (layerDocument) {
            // 弹窗内控件加载初始化
            var content = layerDocument.find("#menu-list-form");
            common.initDivByIdOnLayer({contractNo: contractNo, houseAddress: houseAddress}, content);
            common.initInputByNameOnLayer({password:pwd},content)
        },
        beforeSend: function (layerDocument) {
            var content = layerDocument.find("#menu-list-form");
            var pwd=content.find("input").val();
            sendData.id=lockId;
            sendData.userId=userInfo.id;
            sendData.password=pwd;
            return regPwd.test(pwd);
        },
        resOk: function () {
            //如果更新成功，刷新表格数据
            oTable.fnClearTable();
        }
    });
});
$currentDataTable.off("click",".lock-open").on("click",".lock-open",function (e) {
    var $this=$(this);
    var lockId=$this.attr("data-id");
    var sendData={id:lockId,userId:userInfo.id};
    common.updateLock({
        confirmMsg:"是否确定开锁?",
        path:common.easyOpenPath,
        sendData:sendData,
        listPanel:oTable
    });
});
$currentDataTable.off("click",".getLockInfo").on("click",".getLockInfo",function (e) {
    var lockId=$(this).attr("data-id");
    var sendData={id:lockId,userId:userInfo.id};
    common.updateLock({
        confirmMsg:"确定同步锁信息?",
        path:common.syncLockInfoPath,
        sendData:sendData,
        listPanel:oTable
    });
});
